package com.cmc.cloud.cmclink.doc.service.impl;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.lock.annotation.Lock4j;
import com.cmc.cloud.cmclink.doc.constants.errorconstant.DocBkConfirmPrintSetupErrorCodeConstants;
import com.cmc.cloud.cmclink.doc.entity.DocBkConfirmPrintSetupDO;
import com.cmc.cloud.cmclink.doc.mapper.DocBkConfirmPrintSetupMapper;
import com.cmc.cloud.cmclink.doc.service.DocBkConfirmPrintSetupService;
import com.cmc.cloud.cmclink.doc.vo.docbkconfirmprintsetupvo.*;
import com.cmes.framework.common.exception.util.ServiceExceptionUtil;
import com.cmes.framework.common.pojo.PageResult;
import com.cmes.framework.mybatis.core.query.LambdaQueryWrapperX;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;


/**
 * booking confirm 打印配置 Service 实现类
 *
 * @author 招商轮船-管理员
 */
@Service
@Validated
public class DocBkConfirmPrintSetupServiceImpl implements DocBkConfirmPrintSetupService {

    @Resource
    private DocBkConfirmPrintSetupMapper docBkConfirmPrintSetupMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Lock4j(name = "docBkConfirmPrintSetupUnique", keys = {"#createReqVO.getPortCode"})
    public void createDocBkConfirmPrintSetup(DocBkConfirmPrintSetupCreateReqVO createReqVO) {
        // 插入
        validateUniqueExists(createReqVO, null);
        DocBkConfirmPrintSetupDO docBkConfirmPrintSetup = BeanUtil.copyProperties(createReqVO, DocBkConfirmPrintSetupDO.class);
        docBkConfirmPrintSetupMapper.insert(docBkConfirmPrintSetup);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Lock4j(name = "docBkConfirmPrintSetupUnique", keys = {"#updateReqVO.getPortCode"})
    public void updateDocBkConfirmPrintSetup(DocBkConfirmPrintSetupUpdateReqVO updateReqVO) {
        // 校验存在
        validateDocBkConfirmPrintSetupExists(updateReqVO.getId());
        validateUniqueExists(updateReqVO, updateReqVO.getId());
        // 更新
        DocBkConfirmPrintSetupDO updateObj = BeanUtil.copyProperties(updateReqVO, DocBkConfirmPrintSetupDO.class);
        docBkConfirmPrintSetupMapper.updateById(updateObj);
    }

    @Override
    public void deleteDocBkConfirmPrintSetup(Long id) {
        // 校验存在
        validateDocBkConfirmPrintSetupExists(id);
        // 删除
        docBkConfirmPrintSetupMapper.deleteById(id);
    }

    private void validateUniqueExists(DocBkConfirmPrintSetupBaseVO vo, Long id) {
        LambdaQueryWrapperX<DocBkConfirmPrintSetupDO> queryWrapperX = new LambdaQueryWrapperX<DocBkConfirmPrintSetupDO>()
                .eqIfPresent(DocBkConfirmPrintSetupDO::getPortCode, vo.getPortCode());

        if (id != null) {
            queryWrapperX.ne(DocBkConfirmPrintSetupDO::getId, id);
        }
        if (docBkConfirmPrintSetupMapper.selectCount(queryWrapperX) != 0L) {
            throw ServiceExceptionUtil.exception(DocBkConfirmPrintSetupErrorCodeConstants.DOC_BK_CONFIRM_PRINT_SETUP_PORT_CODE_REPEATED);
        }
    }


    private void validateDocBkConfirmPrintSetupExists(Long id) {
        if (docBkConfirmPrintSetupMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(DocBkConfirmPrintSetupErrorCodeConstants.DOC_BK_CONFIRM_PRINT_SETUP_NOT_EXISTS);
        }
    }

    @Override
    public DocBkConfirmPrintSetupDO getDocBkConfirmPrintSetup(Long id) {
        return docBkConfirmPrintSetupMapper.selectById(id);
    }

    @Override
    public List<DocBkConfirmPrintSetupDO> getDocBkConfirmPrintSetupList(Collection<Long> ids) {
        return docBkConfirmPrintSetupMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<DocBkConfirmPrintSetupDO> getDocBkConfirmPrintSetupPage(DocBkConfirmPrintSetupPageReqVO reqVO) {
        LambdaQueryWrapperX<DocBkConfirmPrintSetupDO> queryWrapperX = new LambdaQueryWrapperX<DocBkConfirmPrintSetupDO>()
                .likeIfPresent(DocBkConfirmPrintSetupDO::getPortCode, reqVO.getPortCode());
        return docBkConfirmPrintSetupMapper.selectPage(reqVO, queryWrapperX);
    }

    @Override
    public DocBkConfirmPrintSetupRespVO getPrintByPortCode(String portCode) {
        DocBkConfirmPrintSetupDO printSetup = new DocBkConfirmPrintSetupDO();
        LambdaQueryWrapperX<DocBkConfirmPrintSetupDO> queryWrapperX = new LambdaQueryWrapperX<DocBkConfirmPrintSetupDO>();
        queryWrapperX.eq(DocBkConfirmPrintSetupDO::getPortCode, portCode);
        return BeanUtil.copyProperties(docBkConfirmPrintSetupMapper.selectOne(queryWrapperX), DocBkConfirmPrintSetupRespVO.class);
    }
}
